/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.gov.ana.controllers;

import br.gov.ana.controllers.util.JsfUtil;
import br.gov.ana.entities.Usina;
import br.gov.ana.facade.UsinaFacade;
import java.io.FileWriter;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

/**
 *
 * @author lucas.nunes
 */
@ManagedBean
@SessionScoped
public class PontosJSONController {

    /**
     * Creates a new instance of PontosJSONController
     */
    @EJB
    UsinaFacade usinaFacade;

    public PontosJSONController() {
    }

    public void gerarPontosJson() {
        try {
            FileWriter file = new FileWriter("p.json");
            List<Usina> usinas = usinaFacade.findAll();

            StringBuilder str = new StringBuilder();
            str.append("[");
            for (Usina u : usinas) {

                if (u.getUsiLatitude() == null || u.getUsiLongitude() == null) {
                    continue;
                }
                str.append("{");

                String id = "\"Id\": " + u.getUsiId() + ",";
                String latitude = "\"Latitude\": " + u.getUsiLatitude().doubleValue() + ",";
                String longitude = "\"Longitude\": " + u.getUsiLongitude().doubleValue() + ",";
                String empresa = "\"Empresa\": \"Empresa: " + u.getOrgaoUsina() + "\",";
                String usina = "\"Usina\": \"Usina: " + u.getUsiTpuId().getTpuNm() + " " + u.getUsiNm() + "\",";
                String latDescricao = "\"LatDescricao\": \"" + JsfUtil.formatCoordenada(u.getUsiLatitude().toString()) + "\",";
                String longDescricao = "\"LongDescricao\": \"" + JsfUtil.formatCoordenada(u.getUsiLongitude().toString()) + "\"";
                
                str.append(id);
                str.append(latitude);
                str.append(longitude);
                str.append(empresa);
                str.append(usina);
                str.append(latDescricao);
                str.append(longDescricao);

                str.append("},");
            }
            str.append("]");

            file.write(str.toString());
            file.close();

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}
